﻿using System;
using System.Collections.Generic;
using System.Text;

using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.Common.Visuals;

namespace BENBUN.Utils
{
    internal class TextUtils
    {
        public static string toDistanceText(double thisDistance, Length.Units defaultUnits)
        {
            string distanceText = string.Empty;

            distanceText = Length.ToString(Length.Convert(thisDistance, Length.Units.Meter, defaultUnits), defaultUnits, "N" + Length.DefaultDecimalPrecision(defaultUnits).ToString() + "u");

            return distanceText;
        }

        public static string toDurationText(TimeSpan thisDuration)
        {
            string displayText = string.Empty;

            if (thisDuration.Hours < 1.0)
            {
                displayText = thisDuration.Minutes.ToString("00") + ":" + thisDuration.Seconds.ToString("00") + " (mm:ss)";
            }
            else
            {
                displayText = Math.Floor(thisDuration.TotalHours) + ":" + thisDuration.Minutes.ToString("00") + ":" + thisDuration.Seconds.ToString("00") + " (hh:mm:ss)";
            }

            return displayText;
        }

        public static string ToSpeedText(double distanceMeters, TimeSpan time, Speed.Units speedUnits, Length.Units lengthUnits, bool bIncludeUnits)
        {
            distanceMeters = Math.Round(distanceMeters, 2);
            switch (lengthUnits)
            {
                case Length.Units.Centimeter:
                case Length.Units.Meter:
                case Length.Units.Kilometer:
                    distanceMeters = Length.Convert(distanceMeters, Length.Units.Meter, Length.Units.Kilometer);
                    break;

                case Length.Units.Inch:
                case Length.Units.Foot:
                case Length.Units.Yard:
                case Length.Units.Mile:
                    distanceMeters = Length.Convert(distanceMeters, Length.Units.Meter, Length.Units.Mile);
                    break;
            }
            if (speedUnits == Speed.Units.Pace)
            {
                if ((time.TotalSeconds == 0.0) || (distanceMeters == 0.0))
                {
                    return "-";
                }
                double d = time.TotalMinutes / distanceMeters;
                if (double.IsNaN(d))
                {
                    return "-";
                }
                // string str = ToString(TimeSpan.FromSeconds(Math.Round(TimeSpan.FromMinutes(d).TotalSeconds, 0)));

                TimeSpan zz = TimeSpan.FromSeconds(Math.Round(TimeSpan.FromMinutes(d).TotalSeconds, 0));
                string str = ToString(zz);
                if (bIncludeUnits)
                {
                    str = str + " " + SpeedUnits(speedUnits, lengthUnits);
                }
                return str;
            }
            if ((time.TotalSeconds == 0.0) || (distanceMeters == 0.0))
            {
                return "0.0";
            }
            string str2 = (distanceMeters / time.TotalHours).ToString("N1");
            if (bIncludeUnits)
            {
                str2 = str2 + " " + SpeedUnits(speedUnits, lengthUnits);
            }
            return str2;
        }

        public static string ToString(TimeSpan time)
        {
            if (time.TotalHours < 1.0)
            {
                return (time.Minutes + ":" + time.Seconds.ToString("00"));
            }
            return string.Concat(new object[] { Math.Floor(time.TotalHours), ":", time.Minutes.ToString("00"), ":", time.Seconds.ToString("00") });
        }

        public static string SpeedUnits(Speed.Units speedUnits, Length.Units lengthUnits)
        {
            if (speedUnits == Speed.Units.Pace)
            {
                switch (lengthUnits)
                {
                    case Length.Units.Centimeter:
                    case Length.Units.Meter:
                    case Length.Units.Kilometer:
                        return CommonResources.Text.LabelMinPerKm;
                }
                return CommonResources.Text.LabelMinPerMile;
            }
            switch (lengthUnits)
            {
                case Length.Units.Centimeter:
                case Length.Units.Meter:
                case Length.Units.Kilometer:
                    return CommonResources.Text.LabelKmPerHour;
            }
            return CommonResources.Text.LabelMilePerHour;

        }
    }
}
